package com.huarui.purchaseOrder.service.provider.task;

import com.huarui.dictionary.DictionaryEnum;
import com.huarui.dictionary.service.DictionaryReadOnlyService.DictionaryResource;
import com.huarui.purchaseOrder.model.PurOrSaleOrder;
import com.huarui.task.model.Task;
import com.huarui.task.service.provider.TaskProvider;

public class PurOrSaleSaveTaskProviderImpl implements TaskProvider{

	private PurOrSaleOrder orders;
		
	
	public PurOrSaleSaveTaskProviderImpl(PurOrSaleOrder orders) {
		super();
		this.orders = orders;
	}
		
	
	@Override
	public Task createTask() {
		return this.saveTask(orders);
	}

	@Override
	public Task updateTask() {
		return null;
	}
	
	private Task saveTask(PurOrSaleOrder orders) {
		
		Task task = new Task();
		
		task.setTableName("orders");
		task.setWorkFlowId(orders.getWorkFlowId());
		task.setTableId(orders.getId());
		
//		Integer preActionCompanyId = PurOrSaleOrder.STATUS_WAIT_SELLER_ACPT.equals(orders.getStatus()) 
//										? orders.getCreatorCompanyId() : null; 
//		String preActionCompanyName = PurOrSaleOrder.STATUS_WAIT_SELLER_ACPT.equals(orders.getStatus())
//										? orders.getCreatorCompanyName() : null;
//		
//		Integer preActorId = PurOrSaleOrder.STATUS_WAIT_SELLER_ACPT.equals(orders.getStatus()) 
//										? orders.getCreatorId() : null;
//										
//		String preActorName = PurOrSaleOrder.STATUS_WAIT_SELLER_ACPT.equals(orders.getStatus()) 
//										? orders.getCreatorName() : null;
										
		Integer preStatus = PurOrSaleOrder.STATUS_WAIT_SELLER_ACPT.equals(orders.getStatus()) 
							|| PurOrSaleOrder.STATUS_WAIT_BUYER_ACPT.equals(orders.getStatus()) 
									? Integer.parseInt(PurOrSaleOrder.STATUS_TMP) : Integer.parseInt(PurOrSaleOrder.STATUS_INITIAL);
		String preStatusName = PurOrSaleOrder.STATUS_WAIT_SELLER_ACPT.equals(orders.getStatus()) 
							|| PurOrSaleOrder.STATUS_WAIT_BUYER_ACPT.equals(orders.getStatus()) 
									? DictionaryResource.getDictionaryValue(DictionaryEnum.GROUP_ORDER_STATUS, preStatus.toString()) : 
									  DictionaryResource.getDictionaryValue(DictionaryEnum.GROUP_ORDER_STATUS, PurOrSaleOrder.STATUS_INITIAL);
									
		task.setPreStatus(preStatus);
		task.setPreStatusId(preStatus);
		task.setPreStatusName(preStatusName);
		
		task.setStatus(Integer.parseInt(orders.getStatus()));
		task.setStatusId(Integer.parseInt(orders.getStatus()));
		task.setStatusName(DictionaryResource.getDictionaryValue(DictionaryEnum.GROUP_ORDER_STATUS, orders.getStatus()));
		
		task.setCreateCompanyId(orders.getCreatorCompanyId());
		task.setCreateCompanyName(orders.getCreatorCompanyName());
		
		task.setCreatorId(orders.getCreatorId());
		task.setCreatorName(orders.getCreatorName());
		
		task.setCurrentCompanyId(orders.getCurrentCompanyId());
		task.setCurrentCompanyName(orders.getCurrentCompanyName());
		
		task.setCurrentorId(orders.getCurrentorId());
		task.setCurrentorName(orders.getCurrentorName());
		
		task.setFinished(Boolean.FALSE);
		if( orders.getCreatorCompanyId().equals(orders.getSellerId()) 
				&& PurOrSaleOrder.STATUS_WAIT_BUYER_ACPT.equals(orders.getStatus()) ) {
			task.setFinished(Boolean.TRUE);
		}
		
		task.setRevoked(Boolean.FALSE);
		

		task.setPreActionCompanyId(null);
		task.setPreActionCompanyName(null);
		
		task.setPreActorId(null);
		task.setPreActorName(null);
		
		
		return task;
	}
	
	
}
